<?php
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 * @version 2014091618
 * @author Max.Yu <max@jipu.com>
 */

namespace Home\Controller;

class IndexController extends HomeController{

  //系统首页
  public function index(){
     //自动登录
     if(!is_login()){
       $uid = cookie('__auto_uid__');
       if($uid){
           $uid = think_decrypt($uid);
           D('Member')->login($uid);
       }
     }
    Cookie('__forward__', $_SERVER['REQUEST_URI']);
    if(isset($_GET['is_app']) && $_GET['is_app'] == 'y'){
          session('IS_APP',true);
    }
    $img = SITE_URL.__IMG__.'/logo-avatar.png';
    if (file_exists($img)) {
      $image_url = $img;
    }else{
      $image_url = SITE_URL.get_cover(C('WAP_LOGO'),'path');
    }
    $share = array(
      'title' => C('WECHAT_INDEX_SHARE_TITLE'),
      'desc' => C('WECHAT_INDEX_SHARE_DESC'),
      'img_url' =>  $img,
      'link' => SITE_URL.U('Index/index', array('sdp_secret' => SHOP_SECRET))
    );
    $this->meta_share = $share;
    // 读取品牌内容
    $bMap['status'] = 1;
    $bMap['recommend'] = 1;
    $order = '`recommend` DESC, `sort` DESC';
    $limit = 17;
    $brand = M('Brand')->where($bMap)->order($order)->limit($limit)->select();
    //广告关键词
    $advKeyword = explode(',',C('ADVER_KEYWORD'));
    $this->assign('brand',$brand);
    $this->assign('advKeyword',$advKeyword);
    $this->display();
  }

  //切换城市
  public function changecity(){
    if(IS_GET && $_GET['type'] =='qie' && $_GET['id']){
      $area_id = I('id');
      $zone = M('Area')->where('id ="'.$area_id.'"')->field('id, title ,sub_name')->find();
      if($zone){
        session('zone' , $zone);
        $this->success('切换成功', Cookie('__forward__'));
        exit();
      }
    }
    $this->areas = D('Shop/Area')->areaZone();
    $this->display();
  }

  // 通栏小广告
  public function ajaxAd(){
      $adMap['tid'] = 5;
      $adMap['status'] = 1;
      $adList = D('Advertise')->where($adMap)->order('sort ASC')->limit(6)->select();
      foreach($adList as $key => $value){
          $adList[$key]['image_src'] = get_cover($value['image'], path);
          $adList[$key]['keys'] = $key%2;
      }
      $this->assign('adList',$adList);
      $rtn['status'] = 1;
      $rtn['info'] = '';
      $rtn['html'] = $this->fetch('ajaxAd');
      exit(json_encode($rtn));
  }

  /**
   * 测试方法
   * @author buddha <buddha@jipu.com>
   * @return [type] [description]
   */
  public function test(){
    phpinfo();
  }


}
